package com.zj.fx.controller.admin;


import com.zj.fx.model.auto.Scan;
import com.zj.fx.service.ScanService;
import com.zj.fx.util.getIpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;


/**
 * @author Administrator
 */
@Controller
@RequestMapping("/ScanController")
public class ScanController {

    @Autowired
    private ScanService scanService;

    @RequestMapping("insertScan")
    @ResponseBody
    public Map<String, Object> insertScan(Scan scan, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        scan.setScanIp(getIpUtil.getIpAddress(request));
        int b = scanService.insertScan(scan);
        if (b > 0) {
            map.put("code", 200);
            return map;
        } else {
            map.put("code", 500);
            return map;
        }
    }

    @RequestMapping("getScan")
    @ResponseBody
    public Map<String, Object> getScan(Scan scan, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        Scan scan1 = new Scan();
        scan1.setScanIp(getIpUtil.getIpAddress(request));
        int b = scanService.insertScan(scan1);
        if (b > 0) {
            Long count = scanService.queryScan(scan);
            scan.setYear("0");
            Long year = scanService.queryScan(scan);
            scan.setYear("");
            scan.setMonth("0");
            Long month = scanService.queryScan(scan);
            scan.setYear("");
            scan.setMonth("");
            scan.setDay("0");
            Long day = scanService.queryScan(scan);
            scan.setYear("");
            scan.setMonth("");
            scan.setDay("");
            scan.setLastMonth("0");
            Long lastMonth = scanService.queryScan(scan);

            map.put("count", count);
            map.put("year", year);
            map.put("month", month);
            map.put("day", day);
            map.put("lastMonth", lastMonth);
        }

        return map;
    }

}
